// 企业知识库 Mock 数据

// 模拟用户信息
export const mockUser = "cs564e5e000156";

// 知识库目录树数据结构 - 左侧全是目录
export interface KnowledgeTreeNode {
  title: string;
  id: string | number;
  children?: KnowledgeTreeNode[];
}

// 知识片段数据结构
export interface KnowledgeFragment {
  know_id: string | number;
  fragment: string;      // 片段标题
  name: string;         // 文件名
  labels: string[];     // 标签
  content?: string;     // 片段内容
  path?: string;        // 片段路径
}

// 文档详情内容结构
export interface DocumentContent {
  title: string;
  content?: string;
  id: string | number;
  children?: DocumentContent[];
}

// Mock 目录树数据 - 左侧全是目录
export const mockDirectoryTree: KnowledgeTreeNode[] = [
  {
    title: "企业制度",
    id: "enterprise_policy",
    children: [
      {
        title: "人力资源管理",
        id: "hr_management",
        children: [
          {
            title: "员工手册",
            id: "employee_handbook"
          },
          {
            title: "薪酬制度", 
            id: "salary_system"
          },
          {
            title: "考勤管理",
            id: "attendance_system"
          }
        ]
      },
      {
        title: "财务管理",
        id: "financial_management",
        children: [
          {
            title: "财务制度",
            id: "financial_system"
          },
          {
            title: "报销流程",
            id: "reimbursement_process"
          }
        ]
      }
    ]
  },
  {
    title: "技术文档",
    id: "technical_docs",
    children: [
      {
        title: "前端开发",
        id: "frontend_dev",
        children: [
          {
            title: "Vue开发规范",
            id: "vue_standards"
          },
          {
            title: "组件库文档",
            id: "component_docs"
          }
        ]
      },
      {
        title: "后端开发",
        id: "backend_dev",
        children: [
          {
            title: "API设计规范",
            id: "api_standards"
          },
          {
            title: "数据库设计",
            id: "database_design"
          }
        ]
      },
      {
        title: "部署运维",
        id: "devops",
        children: [
          {
            title: "Docker部署手册",
            id: "docker_deploy"
          }
        ]
      }
    ]
  },
  {
    title: "项目管理",
    id: "project_management",
    children: [
      {
        title: "项目流程",
        id: "project_process"
      },
      {
        title: "需求管理", 
        id: "requirement_management"
      }
    ]
  },
  {
    title: "培训资料",
    id: "training_materials",
    children: [
      {
        title: "新员工培训",
        id: "new_employee_training"
      },
      {
        title: "技术培训",
        id: "technical_training",
        children: [
          {
            title: "前端技术分享",
            id: "frontend_sharing"
          },
          {
            title: "后端技术分享",
            id: "backend_sharing"
          }
        ]
      }
    ]
  }
];

// Mock 知识片段数据映射
export const mockFragments: Record<string, KnowledgeFragment[]> = {
  // 员工手册的知识片段
  "employee_handbook": [
    {
      know_id: "emp_001",
      fragment: "入职流程规定",
      name: "员工手册.docx",
      labels: ["人事", "入职", "流程"],
      content: `<h2>入职流程规定</h2>
      <p>新员工入职需要完成以下步骤：</p>
      <ol>
        <li>提交个人资料和证件</li>
        <li>签署劳动合同</li>
        <li>办理入职手续</li>
        <li>参加入职培训</li>
        <li>领取办公用品</li>
      </ol>`,
      path: "企业制度-->人力资源管理-->员工手册.docx"
    },
    {
      know_id: "emp_002", 
      fragment: "工作时间与休假",
      name: "员工手册.docx",
      labels: ["人事", "工作时间", "休假"],
      content: `<h2>工作时间与休假</h2>
      <p>标准工作时间：周一至周五 9:00-18:00</p>
      <p>午休时间：12:00-13:00</p>
      <p>年假制度：根据工作年限享受带薪年假</p>`,
      path: "企业制度-->人力资源管理-->员工手册.docx"
    },
    {
      know_id: "emp_003",
      fragment: "员工行为准则", 
      name: "员工手册.docx",
      labels: ["人事", "行为准则", "规范"],
      content: `<h2>员工行为准则</h2>
      <ul>
        <li>遵守公司规章制度</li>
        <li>维护公司形象和利益</li>
        <li>保守商业秘密</li>
        <li>团队合作精神</li>
      </ul>`,
      path: "企业制度-->人力资源管理-->员工手册.docx"
    }
  ],
  
  // 薪酬制度的知识片段
  "salary_system": [
    {
      know_id: "salary_001",
      fragment: "基本薪资结构",
      name: "薪酬制度.docx", 
      labels: ["薪酬", "结构", "基本工资"],
      content: `<h2>基本薪资结构</h2>
      <p>薪资组成：</p>
      <ul>
        <li>基本工资：占总薪资的70%</li>
        <li>绩效奖金：占总薪资的20%</li>
        <li>其他补贴：占总薪资的10%</li>
      </ul>`,
      path: "企业制度-->人力资源管理-->薪酬制度.docx"
    },
    {
      know_id: "salary_002",
      fragment: "绩效考核标准",
      name: "薪酬制度.docx",
      labels: ["绩效", "考核", "标准"],
      content: `<h2>绩效考核标准</h2>
      <p>考核周期：季度考核</p>
      <p>考核维度：</p>
      <ol>
        <li>工作质量 (40%)</li>
        <li>工作效率 (30%)</li>
        <li>团队协作 (20%)</li>
        <li>学习成长 (10%)</li>
      </ol>`,
      path: "企业制度-->人力资源管理-->薪酬制度.docx"
    }
  ],

  // Vue开发规范的知识片段  
  "vue_standards": [
    {
      know_id: "vue_001",
      fragment: "组件命名规范",
      name: "Vue开发规范.md",
      labels: ["前端", "Vue", "命名规范"],
      content: `<h2>组件命名规范</h2>
      <p>Vue组件命名应遵循以下规则：</p>
      <ul>
        <li>使用PascalCase命名法</li>
        <li>组件名应该具有描述性</li>
        <li>避免使用HTML原生标签名</li>
        <li>多单词组件名，避免单个单词</li>
      </ul>
      <pre><code>// 正确示例
const UserProfile = defineComponent({})
const ProductCard = defineComponent({})

// 错误示例  
const user = defineComponent({})
const div = defineComponent({})</code></pre>`,
      path: "技术文档-->前端开发-->Vue开发规范.md"
    },
    {
      know_id: "vue_002",
      fragment: "状态管理规范",
      name: "Vue开发规范.md", 
      labels: ["前端", "Vue", "状态管理"],
      content: `<h2>状态管理规范</h2>
      <p>使用Pinia进行状态管理：</p>
      <ul>
        <li>按功能模块划分store</li>
        <li>使用组合式API风格</li>
        <li>状态命名清晰明确</li>
        <li>action处理异步操作</li>
      </ul>`,
      path: "技术文档-->前端开发-->Vue开发规范.md"
    }
  ],

  // API设计规范的知识片段
  "api_standards": [
    {
      know_id: "api_001", 
      fragment: "RESTful API设计原则",
      name: "API设计规范",
      labels: ["后端", "API", "RESTful"],
      content: `<h2>RESTful API设计原则</h2>
      <p>API设计应遵循REST原则：</p>
      <ol>
        <li>使用HTTP动词表示操作</li>
        <li>使用名词表示资源</li>
        <li>合理使用HTTP状态码</li>
        <li>版本控制策略</li>
      </ol>
      <table>
        <tr><th>HTTP方法</th><th>操作</th><th>示例</th></tr>
        <tr><td>GET</td><td>获取资源</td><td>GET /api/users</td></tr>
        <tr><td>POST</td><td>创建资源</td><td>POST /api/users</td></tr>
        <tr><td>PUT</td><td>更新资源</td><td>PUT /api/users/123</td></tr>
        <tr><td>DELETE</td><td>删除资源</td><td>DELETE /api/users/123</td></tr>
      </table>`,
      path: "技术文档-->后端开发-->API设计规范"
    },
    {
      know_id: "api_002",
      fragment: "错误处理规范", 
      name: "API设计规范",
      labels: ["后端", "API", "错误处理"],
      content: `<h2>错误处理规范</h2>
      <p>统一的错误响应格式：</p>
      <pre><code>{
  "code": 400,
  "message": "参数错误",
  "details": "用户名不能为空",
  "timestamp": "2024-01-01T10:00:00Z"
}</code></pre>
      <p>常用错误码：</p>
      <ul>
        <li>400: 请求参数错误</li>
        <li>401: 认证失败</li>
        <li>403: 权限不足</li>
        <li>404: 资源不存在</li>
        <li>500: 服务器内部错误</li>
      </ul>`,
      path: "技术文档-->后端开发-->API设计规范"
    }
  ],

  // 添加更多目录的知识片段
  "financial_system": [
    {
      know_id: "fin_001",
      fragment: "财务报告制度",
      name: "财务制度",
      labels: ["财务", "报告", "制度"],
      content: `<h2>财务报告制度</h2>
      <p>公司财务报告应遵循以下制度：</p>
      <ul>
        <li>月度财务报表：每月5日前完成</li>
        <li>季度财务分析：每季度末15日前完成</li>
        <li>年度财务审计：每年3月底前完成</li>
      </ul>`,
      path: "企业制度-->财务管理-->财务制度"
    }
  ],

  "project_process": [
    {
      know_id: "proj_001",
      fragment: "项目启动流程",
      name: "项目流程",
      labels: ["项目", "流程", "启动"],
      content: `<h2>项目启动流程</h2>
      <p>项目启动需要完成以下步骤：</p>
      <ol>
        <li>项目立项申请</li>
        <li>可行性分析</li>
        <li>项目组成立</li>
        <li>项目计划制定</li>
        <li>项目启动会议</li>
      </ol>`,
      path: "项目管理-->项目流程"
    }
  ],

  "new_employee_training": [
    {
      know_id: "train_001",
      fragment: "入职第一周培训",
      name: "新员工培训",
      labels: ["培训", "新员工", "入职"],
      content: `<h2>入职第一周培训</h2>
      <p>新员工第一周培训安排：</p>
      <ul>
        <li>第1天：公司介绍和企业文化</li>
        <li>第2天：制度规范学习</li>
        <li>第3天：部门介绍和岗位职责</li>
        <li>第4-5天：业务培训和实操</li>
      </ul>`,
      path: "培训资料-->新员工培训"
    }
  ]
};

// Mock 文档详情内容
export const mockDocumentContent: Record<string, DocumentContent[]> = {
  "employee_handbook": [
    {
      title: "第一章 总则",
      id: "emp_chapter1",
      children: [
        {
          title: "1.1 入职流程规定",
          id: "emp_001",
          content: mockFragments.employee_handbook[0].content
        },
        {
          title: "1.2 工作时间与休假", 
          id: "emp_002",
          content: mockFragments.employee_handbook[1].content
        },
        {
          title: "1.3 员工行为准则",
          id: "emp_003", 
          content: mockFragments.employee_handbook[2].content
        }
      ]
    },
    {
      title: "第二章 权利义务",
      id: "emp_chapter2",
      children: [
        {
          title: "2.1 员工权利",
          id: "emp_004",
          content: `<h2>员工权利</h2>
          <p>员工享有以下权利：</p>
          <ul>
            <li>获得劳动报酬的权利</li>
            <li>休息休假的权利</li>
            <li>获得劳动安全卫生保护的权利</li>
            <li>接受职业技能培训的权利</li>
          </ul>`
        }
      ]
    }
  ],
  
  "vue_standards": [
    {
      title: "命名规范",
      id: "vue_naming",
      children: [
        {
          title: "组件命名规范",
          id: "vue_001",
          content: mockFragments.vue_standards[0].content
        }
      ]
    },
    {
      title: "开发规范", 
      id: "vue_development",
      children: [
        {
          title: "状态管理规范",
          id: "vue_002",
          content: mockFragments.vue_standards[1].content
        }
      ]
    }
  ],

  "api_standards": [
    {
      title: "设计原则",
      id: "api_principles", 
      children: [
        {
          title: "RESTful API设计原则",
          id: "api_001",
          content: mockFragments.api_standards[0].content
        },
        {
          title: "错误处理规范",
          id: "api_002",
          content: mockFragments.api_standards[1].content
        }
      ]
    }
  ]
};

// Mock API 响应生成器
export class KnowledgeMockAPI {
  
  // 获取目录树
  static getDirTree(_params?: any) {
    return {
      code: 200,
      message: "获取成功",
      data: mockDirectoryTree
    };
  }

  // 获取知识片段列表
  static getFragments(params: { kb_path: string }) {
    // 根据路径找到对应的目录ID
    const pathSegments = params.kb_path.split('-->');
    const directoryName = pathSegments[pathSegments.length - 1];
    
    // 目录名到ID的映射
    const directoryToIdMap: Record<string, string> = {
      '员工手册': 'employee_handbook',
      '薪酬制度': 'salary_system',
      '考勤管理': 'attendance_system',
      '财务制度': 'financial_system',
      '报销流程': 'reimbursement_process',
      'Vue开发规范': 'vue_standards',
      '组件库文档': 'component_docs',
      'API设计规范': 'api_standards',
      '数据库设计': 'database_design',
      'Docker部署手册': 'docker_deploy',
      '项目流程': 'project_process',
      '需求管理': 'requirement_management',
      '新员工培训': 'new_employee_training',
      '前端技术分享': 'frontend_sharing',
      '后端技术分享': 'backend_sharing'
    };
    
    // 优先使用目录名匹配，然后使用路径包含匹配
    let docId = directoryToIdMap[directoryName] || '';
    
    // 如果没有直接匹配，尝试在路径中查找
    if (!docId) {
      for (const [key, value] of Object.entries(directoryToIdMap)) {
        if (params.kb_path.includes(key)) {
          docId = value;
          break;
        }
      }
    }
    
    const fragments = mockFragments[docId] || [];
    
    return {
      code: 200,
      message: "获取成功", 
      data: fragments
    };
  }

  // 获取文档详情内容
  static getFirTree(params: { uname: string; dir_path: string }) {
    // 根据路径找到对应的文档内容
    const pathSegments = params.dir_path.split('-->');
    const fileName = pathSegments[pathSegments.length - 1];
    
    const pathToIdMap: Record<string, string> = {
      'employee_handbook': 'employee_handbook',
      '员工手册.docx': 'employee_handbook', 
      'vue_standards': 'vue_standards',
      'Vue开发规范.md': 'vue_standards',
      'api_standards': 'api_standards',
      'API设计规范.md': 'api_standards'
    };
    
    let docId = '';
    for (const [key, value] of Object.entries(pathToIdMap)) {
      if (fileName.includes(key) || params.dir_path.includes(key)) {
        docId = value;
        break;
      }
    }
    
    const content = mockDocumentContent[docId] || [];
    
    return {
      code: 200,
      message: "获取成功",
      data: content
    };
  }

  // 编辑片段内容
  static editFragmentContent(_params: { fragment_path: string; uname: string; content: string }) {
    console.log('编辑片段内容:', _params);
    return {
      code: 200,
      message: "编辑成功",
      data: null
    };
  }

  // 删除片段
  static deleteFragment(params: { uname: string; fragment_path: string }) {
    console.log('删除片段:', params);
    return {
      code: 200,
      message: "删除成功", 
      data: null
    };
  }

  // 创建目录
  static createDir(_params: { uname: string; dir_path: string }) {
    console.log('创建目录:', _params);
    return {
      code: 200,
      message: "创建成功",
      data: null
    };
  }

  // 上传文件
  static uploadFile(_formData: FormData) {
    console.log('上传文件:', _formData);
    return {
      code: 200,
      message: "上传成功",
      data: null
    };
  }
}
